import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from unittest import TestCase


class TestBankPWD(TestCase):
    # 新银行卡没有设置密码
    def testBankPWD1(self):
        expect = "买入失败!,请前往个人信息设置支付密码"
        username = "lisi6"
        pwd = "123456"
        pay = "666667"

        driver = webdriver.Chrome()

        driver.get("http://localhost:90")
        driver.maximize_window()

        # 用户名
        ele1 = driver.find_element(By.ID, "username")
        ele1.send_keys(username)

        # 输入密码
        ele2 = driver.find_element(By.ID, 'password')
        ele2.send_keys(pwd)

        # 点击登陆
        ele3 = driver.find_element(By.ID, 'login_btn')
        ele3.click()
        time.sleep(2)

        # 点击个人理财
        driver.find_element_by_link_text("个人理财").click()
        time.sleep(2)

        # 点击基金理财
        driver.find_element_by_link_text("基金理财").click()

        # 点击支付理财-投资
        driver.find_element_by_xpath("/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td["
                                     "9]/button").click()
        time.sleep(3)

        # 输入支付密码
        driver.find_element_by_css_selector(".layui-layer-content > #password").send_keys(pay)
        driver.implicitly_wait(10)

        # 点击确定
        driver.find_element_by_link_text("确定").click()
        driver.implicitly_wait(5)

        result = driver.find_element_by_xpath("/html/body/div[4]/div[2]/div/div/div/div/div/div/div/div[3]/div/div").text
        time.sleep(4)

        if expect != result:
            print("测试失败")
        else:
            print("买入失败!,请前往个人信息设置支付密码")
            driver.save_screenshot(r"C:\Users\Administrator\PycharmProjects\pythonProject\TEST"
                                   r"\测试截图\银行卡支付\lisi6_123456_666667.png")

        driver.quit()
        self.assertEqual(result, expect)

    # 多个银行卡——默认支付测试
    def testBankPWD2(self):
        expect = "买入成功!"
        username = "lisi7"
        password = "123456"
        pwd = "666666"

        driver = webdriver.Chrome()

        driver.get("http://localhost:90")
        driver.maximize_window()

        # 用户名
        ele1 = driver.find_element(By.XPATH, "//*[@id='username']")
        ele1.send_keys(username)

        # 输入密码
        ele2 = driver.find_element(By.ID, 'password')
        ele2.send_keys(password)

        # 点击登陆
        ele3 = driver.find_element(By.ID, 'login_btn')
        ele3.click()
        time.sleep(2)

        # 点击个人理财
        driver.find_element_by_xpath("//*[@id='leftbaraside']/div[2]/nav/ul/li[2]/a").click()
        time.sleep(2)

        # 点击基金理财
        driver.find_element_by_xpath("//*[@id='leftbaraside']/div[2]/nav/ul/li[2]/ul/li[3]/a").click()

        # 点击支付理财-投资
        driver.find_element_by_xpath("/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td["
                                     "9]/button").click()
        time.sleep(3)

        # 输入支付密码
        driver.find_element(By.CSS_SELECTOR, ".layui-layer-content > #password").send_keys(pwd)
        time.sleep(5)
        # 点击确定
        driver.find_element_by_link_text("确定").click()
        time.sleep(5)

        result = driver.find_element(By.XPATH, '/html/body/div[4]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        time.sleep(4)

        if expect != result:
            print("测试失败")
        else:
            print("买入成功!【默认卡的余额发生改动。】")
            driver.save_screenshot(r"C:\Users\Administrator\PycharmProjects\pythonProject\TEST"
                                   r"\测试截图\银行卡支付\lisi7_123456_666666.png")

        driver.quit()
        self.assertEqual(result, expect)

    # 多个银行卡——默认支付测试
    def testBankPWD3(self):
        expect = "买入成功!"
        username = "lisi7"
        pwd = "123456"
        pay = "666666"

        driver = webdriver.Chrome()

        driver.get("http://localhost:90")
        driver.maximize_window()

        # 用户名
        # ele1 = driver.find_element(By.ID, "username")
        ele1 = driver.find_element(By.XPATH, "//*[@id='username']")
        ele1.send_keys(username)

        # 输入密码
        ele2 = driver.find_element(By.ID, 'password')
        ele2.send_keys(pwd)

        # 点击登陆
        ele3 = driver.find_element(By.ID, 'login_btn')
        ele3.click()
        time.sleep(2)

        # 点击个人理财
        driver.find_element_by_xpath("//*[@id='leftbaraside']/div[2]/nav/ul/li[2]/a").click()
        time.sleep(2)

        # 点击基金理财
        driver.find_element_by_xpath("//*[@id='leftbaraside']/div[2]/nav/ul/li[2]/ul/li[3]/a").click()

        # 点击支付理财-投资
        driver.find_element_by_xpath("/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button").click()
        time.sleep(3)

        # 输入支付密码
        # driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/input').click()
        driver.find_element_by_css_selector(".layui-layer-content > #password").send_keys(pay)
        time.sleep(5)

        # 点击确定
        driver.find_element_by_link_text("确定").click()
        time.sleep(5)
        result = driver.find_element(By.XPATH, '/html/body/div[4]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        time.sleep(4)
        if expect != result:
            print("测试失败")
        else:
            print("买入成功!【第二张默认卡的余额发生改动。】")
            driver.save_screenshot(r"C:\Users\Administrator\PycharmProjects\pythonProject\TEST"
                                   r"\测试截图\银行卡支付\lisi7_123456_666666.png")

        driver.quit()
        self.assertEqual(result, expect)

    # 未绑定银行卡——买入基金理财——广发多元新兴股票
    def testBankPWD4(self):
        expect = "买入失败!，请绑定银行卡"
        username = "lisi5"
        pwd = "123456"
        pay = "666666"

        driver = webdriver.Chrome()

        driver.get("http://localhost:90")
        driver.maximize_window()

        # 用户名
        # ele1 = driver.find_element(By.ID, "username")
        ele1 = driver.find_element(By.XPATH, "//*[@id='username']")
        ele1.send_keys(username)

        # 输入密码
        ele2 = driver.find_element(By.ID, 'password')
        ele2.send_keys(pwd)

        # 点击登陆
        ele3 = driver.find_element(By.ID, 'login_btn')
        ele3.click()
        time.sleep(2)

        # 点击个人理财
        driver.find_element_by_xpath("//*[@id='leftbaraside']/div[2]/nav/ul/li[2]/a").click()
        time.sleep(2)

        # 点击基金理财
        driver.find_element_by_xpath("//*[@id='leftbaraside']/div[2]/nav/ul/li[2]/ul/li[3]/a").click()

        # 点击支付理财-投资
        driver.find_element_by_xpath(
            "/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button").click()
        time.sleep(3)

        # 输入支付密码
        # driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/input').click()
        driver.find_element_by_css_selector(".layui-layer-content > #password").send_keys(pay)
        time.sleep(5)

        # 点击确定
        driver.find_element_by_link_text("确定").click()
        time.sleep(5)
        result = driver.find_element_by_xpath("/html/body/div[4]/div[2]/div/div/div/div/div/div/div/div[3]/div/div").text
        time.sleep(4)
        if expect != result:
            print("测试失败")
        else:
            print("买入失败!，请绑定银行卡")
            driver.save_screenshot(r"C:\Users\Administrator\PycharmProjects\pythonProject\TEST"
                                   r"\测试截图\银行卡支付\lisi5_123456_666666.png")

        driver.quit()
        self.assertEqual(result, expect)
